# -*- coding: utf-8 -*-
"""
MMR route + GPX route scraper
Created on Tue Jul 17 12:36:04 2012

@author: bc6
"""
# edit this to change the keywords on the search page
# words are to be joined together by "+"
KEYWORDS = 'st+louis'


CONSUMER_KEY = 'b513571b80685c74fa95b7755d916c24048889df4' # temporary

import urllib2
import os
from bs4 import BeautifulSoup as BS



# retrieving url for search results of KEYWORDS
# incomplete list, needs to access at API level later
routes_url = "http://www.mapmyrun.com/routes/?keywords=%s"%KEYWORDS
response = urllib2.urlopen(routes_url)
routes_html = response.read()

# use BS to parse the html
# there might be a better way?
soup = BS(routes_html)
imgs = soup.find_all('img', recursive=True)
route_list = []
for each in imgs:
    src = each.get('src')
    if src is not None:
        src_array = src.split('&')
        temp_dict = {}
        for item in src_array:
            str_array = item.split('=')
            if len(str_array) > 1:
                temp_dict[str_array[0]] = str_array[1]
        if "route_id" in temp_dict.keys():
            if not (temp_dict['route_id'] == '%TEMPLATE_ROUTE_ID%'):
                route_list.append(temp_dict)
# route_list should contain a list of dicts
# API for routes
route_api = 'http://www.mapmyrun.com/api_mmf/3.1/routes/get_route_gpx?'
for each in route_list:
    route_id = each['route_id']
    signed_token = each['signed_token']
    print 'route_id: %s, signed_token: %s'%(route_id,signed_token)
    route_api_url = '%sroute_id=%s&signed_token=%s&consumer_key=%s'%(route_api,route_id,signed_token,CONSUMER_KEY)
    print "downloading %s" %route_api_url    
    f = urllib2.urlopen(route_api_url)
    # Open our local file for writing
    dir = '%s'%KEYWORDS    
    if not os.path.exists(dir):
        os.makedirs(dir)
    file_name = '%s/%s.gpx'%(dir,route_id)
    local_file = open(file_name, "wb")
    #Write to our local file
    local_file.write(f.read())
    local_file.close()
print "finished with everything!"